package client_commands

import scala.actors.Actor
import scala.actors.Actor._
import scala.actors.remote.Node
import scala.actors.remote.RemoteActor
import scala.actors.remote.RemoteActor._
import objects.File3
import executors.UploadFileExecutor

/**
 * Listens to uploads of files from clients.<br>
 * Works as a remote actor that listens to messages on a given port.<br>
 * @param - port to listen on.<br>
 * @param - executor : Actor
 */
class UploadFileActor(port: Int, executor : Actor) extends Actor {

  /**
   * The acting method of the actor.<br>
   * Receives messages about uploaded files from clients and then sends <br>
   * the information forwards to be processed against the data-base, the disk, and other clients. 
   */
  def act() {
    alive(port)
    register('UploadFile, self)

    loop {
      react {
        case (uid : Long, cid : Long, name : String, parent : Long, size : Long, code : Long, bytes : Array[Byte]) =>
          {
            val receivedFile = new File3(uid, cid, name, parent, size, code, bytes)
            println("processing uploaded file "+name)
            executor ! receivedFile
          }
        case "Quit" => {
          println("UploadFileActor stopped")
          executor ! "Quit"
          exit()
        }
        case _ =>
          println("UploadFileActor received unknown message")
      }
    }
  }

}